package com.jason.www.jdk8;

import org.junit.Test;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class F_DateTimeAPI {
    //1.LocalDate LocalTime LocalDateTime
    @Test
    public void test1(){
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        LocalDateTime ldt2 = LocalDateTime.of(2017, 05, 21, 21, 43, 55, 33);
        System.out.println(ldt2);

        LocalDateTime ldt3 = ldt.plusYears(3);//当前时间加多三年
        System.out.println(ldt3);

        LocalDateTime ldt4 = ldt.minusMonths(5);//当前时间减去5个月
        System.out.println(ldt4);
    }


    //2.Instant 时间戳  用于“时间戳”的运算。它是以Unix元年(传统的设定为UTC时区1970年1月1日午夜时分)开始所经历的描述进行运算。
    @Test
    public void test2(){
        Instant now = Instant.now();
        System.out.println(now);

        OffsetDateTime atOffset = now.atOffset(ZoneOffset.ofHours(6));
        System.out.println(atOffset);

        Instant ins = Instant.ofEpochSecond(60);
        System.out.println(ins);
    }


    //3.Duration    用于计算两个“时间”间隔
    @Test
    public void test3(){
        Instant now = Instant.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        Instant now2 = Instant.now();
        //计算时间差
        Duration duration = Duration.between(now, now2);
        System.out.println(duration.getSeconds());
        System.out.println("----------------");

        LocalTime lt1 = LocalTime.now();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        LocalTime lt2 = LocalTime.now();
        System.out.println(Duration.between(lt1, lt2).toMillis());
    }

    //4.Period  用于计算两个“日期”间隔
    @Test
    public void test4(){
        LocalDate ld1 = LocalDate.of(2017, 1, 1);
        LocalDate ld2 = LocalDate.now();
        Period period = Period.between(ld1, ld2);
        System.out.println(period.getYears());
        System.out.println(period.getMonths());
        System.out.println(period.getDays());
    }

    //5.TemporalAdjuster:时间校正器
    @Test
    public void test5(){
        LocalDateTime ldt = LocalDateTime.now();
        System.out.println(ldt);

        LocalDateTime ldt2 = ldt.withDayOfMonth(8);
        System.out.println(ldt2);

        LocalDateTime ldt3 = ldt.with(TemporalAdjusters.next(DayOfWeek.SATURDAY));
        System.out.println(ldt3);

        //自定义
        LocalDateTime ldt5 =  ldt.with((l) -> {
            LocalDateTime ldt4 = (LocalDateTime) l;
            DayOfWeek dow = ldt4.getDayOfWeek();
            if (dow.equals(DayOfWeek.FRIDAY)) {
                return ldt4.plusDays(3);
            }else if (dow.equals(DayOfWeek.SATURDAY)) {
                return ldt4.plusDays(2);
            } else {
                return ldt4.plusDays(1);
            }
        });
        //下个工作日
        System.out.println(ldt5);
    }

    //DateTimeFormatter:格式化时间/日期
    @Test
    public void test6(){
        DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
        LocalDateTime ldt = LocalDateTime.now();

        System.out.println(ldt);
        String format = ldt.format(dtf);
        System.out.println(format);
        System.out.println("------------");
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
        String format2 = dtf2.format(ldt);
        System.out.println(format2);

        LocalDateTime ldt2 = LocalDateTime.parse(format2,dtf2);
        System.out.println(ldt2);
    }

    //ZonedDate ZoneTime ZoneDateTime  时区的处理
    @Test
    public void test7(){
        LocalDateTime ldt = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
        System.out.println(ldt);

        LocalDateTime ldt2 = LocalDateTime.now(ZoneId.of("Europe/Tallinn"));
        ZonedDateTime zdt = ldt2.atZone(ZoneId.of("Asia/Shanghai"));
        System.out.println(zdt);
    }
}

